﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Gif.Components;
using System.Collections.Generic;
using QzEbook.Common;

public partial class SaveData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //int v = 0;
        BufferedStream inputStream = null;
        MemoryStream ms = null;
        try
        {
            inputStream = new BufferedStream(Request.InputStream);
            //ms = new MemoryStream();
            //Byte[] bytes = new Byte[1024];
            //while ((v = inputStream.Read(bytes, 0, 1024)) > 0)
            //{
            //    ms.Write(bytes, 0, v);
            //}
            //ms.Flush();

            int id = 0;

            if (Session["swfAutoId"] != null)
            {
                id = (int)Session["swfAutoId"];
                id++;
            }
            Session["swfAutoId"] = id;

            string savePath=Server.MapPath(Config.UserBookSavePath +id.ToString() + ".jpg");
            System.Drawing.Image image = System.Drawing.Image.FromStream(inputStream);
            image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);

            Thumbnail thumb = new Thumbnail(id.ToString(),savePath);

            List<Thumbnail> thumbnails = Session["swf2images"] as List<Thumbnail>;
            if (thumbnails == null)
            {
                thumbnails = new List<Thumbnail>();
                Session["swf2images"] = thumbnails;
            }
            thumbnails.Add(thumb);


            int len = (int)Session["frameCount"];
            if (id >=len)
            {
                Session.Remove("swfAutoId");
                Session.Remove("swf2images");


                AnimatedGifEncoder gif = new AnimatedGifEncoder();
                String outputFilePath = Server.MapPath(Config.UserBookSavePath + "auto.gif");
                gif.Start(outputFilePath);
                gif.SetFrameRate(12);
                gif.SetQuality(8);
                gif.SetRepeat(0);


                //foreach (Thumbnail item in thumbnails)
                //{
                for(int i=1;i<len;i++){
                    Thumbnail item = thumbnails[i];
                    System.Drawing.Image addimage = System.Drawing.Image.FromFile(item.Path);
                    gif.AddFrame(addimage);
                    addimage.Dispose();
                    File.Delete(item.Path);
                }

                gif.Finish();

            }

        }
        catch (Exception ex)
        {
            Response.StatusCode = 500;
            Response.Write(ex.Message);
            Response.End();
        }
        finally
        {
            if (inputStream != null)
            {
                inputStream.Close();
            }
            if (ms != null)
            {
                ms.Close();
            }
        }
    }
}
